package com.leoman.controller;

import com.leoman.common.factory.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Configue;
import com.leoman.core.Constant;
import com.leoman.entity.Area;
import com.leoman.entity.Image;
import com.leoman.entity.Question;
import com.leoman.service.AreaService;
import com.leoman.service.ImageService;
import com.leoman.service.QuestionService;
import com.leoman.utils.WebUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反馈问题
 * Created by 涂奕恒 on 2015/7/29.
 */
@Controller
@RequestMapping(value = "admin/question")
public class QuestionController extends CommonController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private ImageService imageService;

    @RequestMapping(value = "/index")
    public String index() {
        return "反馈问题列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer typeId,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Question> page = questionService.pageByParam(typeId, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping(value = "/show")
    public String show(ModelMap model, Integer id) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map;
        Area areaThree = null;
        Area areaTwo = null;
        Area areaOne = null;

        Question question = new Question();
        if (null != id) {
            question = questionService.getById(id);
        }

        model.addAttribute("question", question);
        if (null != id && null != question) {
            if (null != question.getArea()) {
                areaThree = question.getArea();
                if (areaThree.getLevel() == 3) {
                    areaTwo = areaService.getById(areaThree.getParentId());
                    areaOne = areaService.getById(areaTwo.getParentId());

                    model.addAttribute("threeAreaId", areaThree.getId());
                    model.addAttribute("twoAreaId", areaTwo.getId());
                    model.addAttribute("firstAreaId", areaOne.getId());
                } else if (areaThree.getLevel() == 2) {
                    areaTwo = areaThree;
                    areaOne = areaService.getById(areaTwo.getParentId());
                    model.addAttribute("twoAreaId", areaTwo.getId());
                    model.addAttribute("firstAreaId", areaOne.getId());
                } else {
                    areaOne = areaThree;
                    model.addAttribute("firstAreaId", areaOne.getId());
                }
            }

            // 如果反馈问题不为空，则查询对应的图片集合
            List<Image> imageList = imageService.findListByQuestionId(question.getId());
            for (Image image : imageList) {
                map = new HashMap<>();
                map.put("id", image.getId());
                map.put("url", "http://192.168.100.40:85/files/upload/" + image.getUrl());

                list.add(map);
            }
        }

        model.addAttribute("imageList", JSONArray.fromObject(list));

        if (null != id && null != question && question.getStatus().equals(Constant.STATUS_002)) {
            model.addAttribute("areaThree", areaThree);
            model.addAttribute("areaTwo", areaTwo);
            model.addAttribute("areaOne", areaOne);
            return "反馈问题详情2";
        } else {
            return "反馈问题详情";
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(Integer id, Integer type, String recordDate,
                        Integer areaId, String descriptionOne,
                        String descriptionTwo, Integer scoreOne,
                        Integer scoreTwo, Integer scoreThree,
                        Integer scoreFour, Integer scoreFive,
                        Integer scoreSix, Integer scoreSeven,
                        String nameOne, String sectionOne,
                        String descriptionThree, Integer scoreEight,
                        Integer scoreNine, Integer scoreTen,
                        String nameTwo, String sectionTwo, String planDateOne,
                        String actualDateOne, Integer isCut, Integer status,
                        String tempAddImageIds, String tempDelImageIds) {
        try {
            return questionService.updateQuestion(id, type,
                    recordDate, areaId, descriptionOne,
                    descriptionTwo, scoreOne, scoreTwo,
                    scoreThree, scoreFour, scoreFive, scoreSix,
                    scoreSeven, nameOne, sectionOne, descriptionThree,
                    scoreEight, scoreNine, scoreTen, nameTwo,
                    sectionTwo, planDateOne, actualDateOne,
                    isCut, status, tempAddImageIds, tempDelImageIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer id) {
        try {
            questionService.deleteById(id);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/sendInfo")
    @ResponseBody
    public Integer sendInfo(Integer id, String section, String name, String sectionId, String userId) {
        try {
            return questionService.sendInfo(id, section, name, sectionId, userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/closeInfo")
    @ResponseBody
    public Integer closeInfo(Integer id,
                             String descriptionFour,
                             Integer scoreEleven,
                             Integer scoreTwelve,
                             Integer scoreThirteen,
                             String nameThree,
                             String sectionThree,
                             String planDateTwo,
                             String actualDateTwo) {
        try {
            Question question = questionService.getById(id);

            question.setDescriptionFour(descriptionFour);
            question.setScoreEleven(scoreEleven);
            question.setScoreTwelve(scoreTwelve);
            question.setScoreThirteen(scoreThirteen);
            question.setNameThree(nameThree);
            question.setSectionThree(sectionThree);
            question.setPlanDateTwo(planDateTwo);
            question.setActualDateTwo(actualDateTwo);
            question.setStatus(Constant.STATUS_002);

            questionService.update(question);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
